package com.llkj.contractmanagement.main;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.promeg.pinyinhelper.Pinyin;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.actor.ActorFragment;
import com.llkj.contractmanagement.contractmanagement.contract.ContractFragment;
import com.llkj.contractmanagement.contractmanagement.mine.MineFragment;
import com.llkj.contractmanagement.main.BaseActivity;

import butterknife.Bind;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_contract)
    TextView tvContract;
    @Bind(R.id.tv_actor)
    TextView tvActor;
    @Bind(R.id.tv_mine)
    TextView tvMine;
    @Bind(R.id.rl_contract)
    RelativeLayout rlContract;
    @Bind(R.id.rl_actor)
    RelativeLayout rlActor;
    @Bind(R.id.rl_mine)
    RelativeLayout rlMine;
    private ActorFragment actorFragment;
    private ContractFragment contractFragment;
    private MineFragment mineFragment;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        Pinyin.init(Pinyin.newConfig());
        init();
    }

    private void init() {
        setListener();
        setSelection(0);
    }

    private void setListener() {
        rlContract.setOnClickListener(this);
        rlActor.setOnClickListener(this);
        rlMine.setOnClickListener(this);
    }

    private void setSelected(View view) {
        rlContract.setSelected(false);
        rlActor.setSelected(false);
        rlMine.setSelected(false);
        view.setSelected(true);
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0:// 合同
                setSelected(rlContract);
                if (contractFragment == null) {
                    contractFragment = new ContractFragment();
                    fragmentTransaction.add(R.id.fl_content, contractFragment);
                } else {
                    fragmentTransaction.show(contractFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1:// 艺人
                setSelected(rlActor);
                if (actorFragment == null) {
                    actorFragment = new ActorFragment();
                    fragmentTransaction.add(R.id.fl_content, actorFragment);
                } else {
                    fragmentTransaction.show(actorFragment);
                }
                fragmentTransaction.commit();
                break;
            case 2:// 我的
                setSelected(rlMine);
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.fl_content, mineFragment);
                } else {
                    fragmentTransaction.show(mineFragment);
                }
                fragmentTransaction.commitAllowingStateLoss();
                break;
        }

    }

    /**
     * 隐藏所有的fragment
     *
     * @param fragmentTransaction
     */
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (contractFragment != null) {
            fragmentTransaction.hide(contractFragment);
        }
        if (actorFragment != null) {
            fragmentTransaction.hide(actorFragment);
        }
        if (mineFragment != null) {
            fragmentTransaction.hide(mineFragment);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_contract://合同
                setSelection(0);
                break;
            case R.id.rl_actor://艺人
                setSelection(1);
                break;
            case R.id.rl_mine://我的
                setSelection(2);
                break;
        }
    }

}
